package com.sailfish.springbootdemo.dao.db7;

import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbTaskTemplateData;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface TbTaskTemplateDataDao {

    @Insert("<script>" +
            "insert into " + DB7TableName.TB_TASK_TEMPLATE_DATA + " " +
            "( "+
            "flow_no, " +
            "flow_level, " +
            "version, " +
            "template_type, " +
            "parent_id, " +
            "param_name, " +
            "param_name_en, " +
            "param_key, " +
            "parent_param_key, " +
            "param_type, " +
            "param_value, " +
            "level, " +
            "select_level, " +
            "parent_select_id, " +
            "tip, " +
            "remark, " +
            "create_user, " +
            "create_time, " +
            "update_user, " +
            "update_time, " +
            "sort, " +
            "row_sort, " +
            "is_value " +
            ")"+
            "values "+
            "<foreach collection=\"entities\" item=\"entity\" separator=\",\">" +
            "(" +
            "#{entity.flow_no}, " +
            "#{entity.flow_level}, " +
            "#{entity.version}, " +
            "#{entity.template_type}, " +
            "#{entity.parent_id}, " +
            "#{entity.param_name}, " +
            "#{entity.param_name_en}, " +
            "#{entity.param_key}, " +
            "#{entity.parent_param_key}, " +
            "#{entity.param_type}, " +
            "#{entity.param_value}, " +
            "#{entity.level}, " +
            "#{entity.select_level}, " +
            "#{entity.parent_select_id}, " +
            "#{entity.tip}, " +
            "#{entity.remark}, " +
            "#{entity.create_user}, " +
            "#{entity.create_time}, " +
            "#{entity.update_user}, " +
            "#{entity.update_time}, " +
            "#{entity.sort}, " +
            "#{entity.row_sort}, " +
            "#{entity.is_value})"+
            "</foreach>"+
            "</script>"
    )
    Integer insertTaskTemplateDataBatch(@Param("entities") List<TbTaskTemplateData> entities);


    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " where 1=1" +
            "<if test='flow_no != null and flow_no!=\"\"'>" +
            " and flow_no = #{flow_no}" +
            "</if>" +
            "order by sort asc, id asc "+
            "</script>")
    List<TbTaskTemplateData> getTaskTemplateDataByFlowNo(@Param("flow_no") String flow_no);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " where 1=1 AND (param_name='器件编码' or param_name='器件封装') AND row_sort=0 " +
            "<if test='flow_no != null and flow_no!=\"\"'>" +
            " and flow_no = #{flow_no}" +
            "</if>" +
            "order by id asc "+
            "</script>")
    List<TbTaskTemplateData> getTaskTemplateDataByFlowNo2(@Param("flow_no") String flow_no);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " where 1=1 AND parent_param_key='deviceType' AND row_sort=-1 " +
            "<if test='flow_no != null and flow_no!=\"\"'>" +
            " and flow_no = #{flow_no}" +
            "</if>" +
            "order by id asc "+
            "</script>")
    List<TbTaskTemplateData> getTaskTemplateDataByFlowNo3(@Param("flow_no") String flow_no);

    @Update("<script>" +
            "update " + DB7TableName.TB_TASK_TEMPLATE_DATA + " " +
            "set " +
            "param_value = #{entity.param_value}, " +
            "update_user = #{entity.update_user}, " +
            "update_time = #{entity.update_time}, " +
            "row_sort = #{entity.row_sort} " +
            "where flow_no = #{entity.flow_no}" +
            "and param_key = #{entity.param_key} " +
            "and row_sort = #{entity.row_sort_old}" +
            "</script>"
    )
    Integer updateTaskTemplateData(@Param("entity") TbTaskTemplateData entity);

    @Update("<script>" +
            "update " + DB7TableName.TB_TASK_TEMPLATE_DATA + " " +
            "set " +
            "param_value = #{value} " +
            "where id = #{id}" +
            "</script>"
    )
    Integer updateTaskTemplateDataValue(@Param("value") String value,@Param("id") Integer id);


    @Delete("<script>" +
            "delete from " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " where flow_no = #{entity.flow_no} " +
            " and row_sort=#{entity.row_sort} " +
            " and parent_param_key= #{entity.parent_param_key}"+
            "</script>")
    Integer deleteTaskTemplateData(@Param("entity") TbTaskTemplateData entity);

    @Update("<script>" +
            "update " + DB7TableName.TB_TASK_TEMPLATE_DATA + " " +
            "set " +
            "param_value = #{entity.param_value}, " +
            "update_user = #{entity.update_user}, " +
            "update_time = #{entity.update_time}, " +
            "row_sort = #{entity.row_sort} " +
            "where flow_no = #{entity.flow_no}" +
            "and parent_param_key = #{entity.parent_param_key} " +
            "and row_sort = #{entity.row_sort_old}" +
            "</script>"
    )
    Integer deleteTaskTemplateDataU(@Param("entity") TbTaskTemplateData entity);

    @Delete("<script>" +
            "delete from " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " where flow_no = #{flow_no} " +
            "</script>")
    Integer deleteTaskTemplateDataByFlowNo(@Param("flow_no") String flow_no);


    @Select("<script> " +
            "SELECT row_sort " +
            "FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " WHERE 1=1 " +
            "<if test='flowNo != null and flowNo != \"\"'> " +
            "AND flow_no = #{flowNo} " +
            "</if> " +
            " order by row_sort desc limit 1" +
            "</script>")
    Integer getMaxRowSortByLimit(
            @Param("flowNo") String flowNo);

//    @Select("<script> " +
//            "SELECT * " +
//            "FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA + " t2 " +
//            "WHERE 1=1 " +
//            "<if test='templateType != null and templateType != \"\"'> " +
//            "    AND t2.template_type = #{templateType} " +
//            "</if> " +
//            "<if test='version != null and version != \"\"'> " +
//            "    AND t2.version = #{version} " +
//            "</if> " +
//            "AND row_sort != -1" +
//            "<if test='hasValidConditions'> " +
//            "    AND NOT EXISTS ( " +
//            "        SELECT 1 " +
//            "        FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA + " t1 " +
//            "        WHERE t1.id = t2.id " +
//            "        <if test='conditions != null and conditions.size() > 0'> " +
//            "            AND ( " +
//            "                <foreach collection='conditions' item='condition' separator='OR'> " +
//            "                    (t1.param_key = #{condition.key} AND COALESCE(t1.param_value, '') NOT LIKE CONCAT('%', #{condition.value}, '%'))" +
//            "                </foreach> " +
//            "            ) " +
//            "        </if> " +
//            "    ) " +
//            "</if> " +
//            "</script>")
//    List<TbTaskTemplateData> getTaskTemplateDataByLimit(
//            @Param("templateType") String templateType,
//            @Param("version") String version,
//            @Param("conditions") List<Map<String, String>> conditions,
//            @Param("hasValidConditions") boolean hasValidConditions
//    );
    @Select("<script>" +
            "SELECT * FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " WHERE 1=1 " +
            "<if test='templateType != null and templateType != \"\"'> " +
            "    AND template_type = #{templateType} " +
            "</if> " +
            "<if test='version != null and version != \"\"'> " +
            "    AND version = #{version} " +
            "</if> " +
            "</script>")
    List<TbTaskTemplateData> getTaskTemplateDataByLimit(
            @Param("templateType") String templateType,
            @Param("version") String version
    );

    @Select("<script>" +
            "SELECT * FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " WHERE 1=1 " +
            "<if test='condition != null'> " +
            "    AND ( "  +
            "            (param_key = #{condition.key} AND param_value LIKE CONCAT('%', #{condition.value}, '%'))" +
            "    ) " +
            "</if>" +
            "</script>")
    List<TbTaskTemplateData> getValidByCondition(
            @Param("condition") Map<String, String> condition
    );

    @Select("<script> " +
            "SELECT * FROM " + DB7TableName.TB_TASK_TEMPLATE_DATA +
            " WHERE 1=1 " +
            "<if test='flowNos != null and flowNos.size() > 0'>" +
            " AND flow_no IN " +
            "<foreach item='flowNo' collection='flowNos' open='(' separator=',' close=')'>" +
            "#{flowNo}" +
            "</foreach>" +
            "</if>" +
            " ORDER BY sort ASC, id ASC" +
            "</script>")
    List<TbTaskTemplateData> getTaskTemplateDataByFlowNos(@Param("flowNos") List<String> flowNos);
}

